package com.wwt.springboot_vue.controller;

import com.wwt.springboot_vue.bean.User;
import com.wwt.springboot_vue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("findAll")
    public List<User> findAllGoods(){
        List<User> list = userService.findAll();
        return list;
    }

    @GetMapping("findByStatus")
    public List<User> findByStatus(String info){
        List<User> list = userService.findByStatus(info);
        return list;
    }

    @GetMapping("findById")
    public User findById(int user_id){
        User user = userService.findById(user_id);
        return user;
    }

    @GetMapping("findByLimit")
    public List<User> findByLimit(int start, int size){
        return userService.findByLimit(start, size);
    }

    @GetMapping("findByStatusLimit")
    public List<User> findByStatusLimit(String info, int start, int size){
        return userService.findByStatusLimit(info, start, size);
    }

    @GetMapping("delete")
    public Map<String,Object> delete(int user_id){
        Map<String,Object> map = new HashMap<>();
        try {
            userService.delete(user_id);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

    @PostMapping("add")
    public Map<String,Object> add(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        try {
            userService.add(user);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }

    @PostMapping("update")
    public Map<String,Object> update(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        try {
            userService.update(user);
            map.put("success",true);
        }catch (Exception e){
            map.put("success",false);
        }
        return map;
    }
}
